package sh.grapendaal.tsuushin.module.uno.engine.event.dispatcher;

import sh.grapendaal.tsuushin.module.uno.engine.domain.achievement.Achievement;
import sh.grapendaal.tsuushin.module.uno.engine.dto.GameAction;
import sh.grapendaal.tsuushin.module.uno.engine.dto.GameState;
import sh.grapendaal.tsuushin.module.uno.engine.event.GameEvent;
import sh.grapendaal.tsuushin.module.uno.engine.event.listener.EventListener;
import sh.grapendaal.tsuushin.module.uno.engine.event.listener.GameEventListener;

public abstract class GameEventDispatcher extends EventDispatcher<GameEvent> {

	public void dispatchPlayerJoin(final GameState gameState) {
		dispatchEvent(new EventRunnable<GameEvent>() {
			@Override
			public void run(EventListener<GameEvent> listener) {
				listener.onPlayerJoin(new GameEvent(GameAction.PLAYER_JOINED, gameState));
			}
		});
	}
	
	public void dispatchPlayerLeave(final GameState gameState) {
		dispatchEvent(new EventRunnable<GameEvent>() {
			@Override
			public void run(EventListener<GameEvent> listener) {
				listener.onPlayerLeave(new GameEvent(GameAction.PLAYER_LEFT, gameState));
			}
		});
	}
	
	public void dispatchGameStart(final GameState gameState) {
		dispatchEvent(new EventRunnable<GameEvent>() {
			@Override
			public void run(EventListener<GameEvent> listener) {
				listener.onGameStart(new GameEvent(GameAction.GAME_STARTED, gameState));
			}
		});
	}
	
	public void dispatchGameStop(final GameState gameState) {
		dispatchEvent(new EventRunnable<GameEvent>() {
			@Override
			public void run(EventListener<GameEvent> listener) {
				listener.onGameStop(new GameEvent(GameAction.GAME_STOPPED, gameState));
			}
		});
	}

	public void dispatchGameEnd(final GameState gameState) {
		dispatchEvent(new EventRunnable<GameEvent>() {
			@Override
			public void run(EventListener<GameEvent> listener) {
				((GameEventListener) listener).onGameEnd(new GameEvent(GameAction.GAME_ENDED, gameState));
			}
		});
	}

	public void dispatchCardPlay(final GameState gameState) {
		dispatchEvent(new EventRunnable<GameEvent>() {
			@Override
			public void run(EventListener<GameEvent> listener) {
				listener.onCardPlay(new GameEvent(GameAction.CARD_PLAYED, gameState));
			}
		});
	}
	
	public void dispatchCardDraw(final GameState gameState) {
		dispatchEvent(new EventRunnable<GameEvent>() {
			@Override
			public void run(EventListener<GameEvent> listener) {
				listener.onCardDraw(new GameEvent(GameAction.CARD_DRAWN, gameState));
			}
		});
	}
	
	public void dispatchCardPass(final GameState gameState) {
		dispatchEvent(new EventRunnable<GameEvent>() {
			@Override
			public void run(EventListener<GameEvent> listener) {
				listener.onCardPass(new GameEvent(GameAction.CARD_PASSED, gameState));
			}
		});
	}
	
	public void dispatchTurnStart(final GameState gameState) {
		dispatchEvent(new EventRunnable<GameEvent>() {
			@Override
			public void run(EventListener<GameEvent> listener) {
				((GameEventListener) listener).onTurnStart(new GameEvent(GameAction.TURN_BEGAN, gameState));
			}
		});
	}
	
	public void dispatchTurnSkip(final GameState gameState) {
		dispatchEvent(new EventRunnable<GameEvent>() {
			@Override
			public void run(EventListener<GameEvent> listener) {
				((GameEventListener) listener).onTurnSkip(new GameEvent(GameAction.TURN_SKIPPED, gameState));
			}
		});
	}

	public void dispatchTurnEnd(final GameState gameState) {
		dispatchEvent(new EventRunnable<GameEvent>() {
			@Override
			public void run(EventListener<GameEvent> listener) {
				((GameEventListener) listener).onTurnEnd(new GameEvent(GameAction.TURN_ENDED, gameState));
			}
		});
	}
	
	public void dispatchAchievementUnlocked(final GameState gameState, final Achievement achievement) {
		dispatchEvent(new EventRunnable<GameEvent>() {
			@Override
			public void run(EventListener<GameEvent> listener) {
				((GameEventListener) listener).onAchievementUnlock(new GameEvent(GameAction.ACHIEVEMENT_UNLOCKED, gameState), achievement);
			}
		});
	}

}
